package model.dao.vatnuoi;

import java.util.List;
import model.entity.VatNuoi;
import model.initialize.Initialize;
import model.util.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Session;


public class VatNuoiDAO {
  
    public static String LOAD_ALL_VATNUOI_THEO_KH = "FROM VatNuoi as vn WHERE KH_MA =";
	
    public static VatNuoi timTheoMaVatNuoi(int maVatNuoi){
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<VatNuoi> list = session.createQuery("FROM VatNuoi AS vatNuoi WHERE vatNuoi.vnMa = ? ")
                .setInteger(0, maVatNuoi)
                .list();
        for (VatNuoi vatNuoi : list) {
            Initialize.initializeVatNuoi(vatNuoi);
        }
        if(list.size()>0){
            return list.get(0);
        }
        return null;
        
    }
	
	public static final List<VatNuoi> timVNTheoKH(int maKH){
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    List<VatNuoi> list = session.createQuery(LOAD_ALL_VATNUOI_THEO_KH +"'"+ maKH +"'").list();
    for (VatNuoi vatNuoi : list) {
        Hibernate.initialize(vatNuoi.getKhachHang());
        Hibernate.initialize(vatNuoi.getChungLoai());
        Hibernate.initialize(vatNuoi.getChungLoai().getLoai());
    }
        return list;
    
}
    
    
}
